package com.kyrie.lib_sys.application.timeApplys.mapper;

import com.kyrie.lib_sys.application.timeApplys.pojo.entity.TimeRecord;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface TimeRecordMapper {
    /**
     * 添加工时时间记录
     *
     * @param record 时间记录
     * @return 添加结果
     */
    @Insert("INSERT INTO time_record(employee_id, prepare_time, travel_time, start_time, complete_time) " +
            "VALUES(#{employeeId}, #{prepareTime}, #{travelTime}, #{startTime}, #{completeTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(TimeRecord record);


    /**
     * 查询指定员工所有时间记录
     *
     * @param employeeId 员工ID
     * @return 时间记录列表
     */
    @Select("SELECT * FROM time_record WHERE employee_id = #{employeeId} " +
            "ORDER BY prepare_time ASC")
    List<TimeRecord> findByEmployeeId(Long employeeId);


    /**
     * 批量插入时间记录
     *
     * @param employeeId,start,end 时间记录列表
     * @return 插入的记录数
     */
    @Select("SELECT * FROM time_record WHERE employee_id = #{employeeId} " +
            "AND ((prepare_time BETWEEN #{start} AND #{end}) OR " +
            "(complete_time BETWEEN #{start} AND #{end}) OR " +
            "(prepare_time <= #{start} AND complete_time >= #{end})) " +
            "ORDER BY prepare_time ASC")
    List<TimeRecord> findByEmployeeIdAndTimeRange(@Param("employeeId") Long employeeId,
                                                  @Param("start") LocalDateTime start,
                                                  @Param("end") LocalDateTime end);



    /**
     * 批量插入时间记录
     * @param newRecords 时间记录列表
     */
    @Insert("<script>" +
            "INSERT INTO time_record(employee_id, prepare_time, travel_time, start_time, complete_time) VALUES " +
            "<foreach collection='newRecords' item='record' separator=','>" +
            "(#{record.employeeId}, #{record.prepareTime}, #{record.travelTime}, #{record.startTime}, #{record.completeTime})" +
            "</foreach>" +
            "</script>")
    void insertBatch(@Param("newRecords") List<TimeRecord> newRecords);

}